//global
var oCurElmt=null;
var oTop=null;

//元素类型.[可获得焦点][可选定][Must zReg]
aElement=new Array();
aElement["text"]		=new Array(true,true,true);
aElement["submit"]		=new Array(true,false,false);
aElement["reset"]		=new Array(true,false,false);
aElement["button"]		=new Array(true,false,false);
aElement["select"]		=new Array(true,false,true);
aElement["checkbox"]	=new Array(true,false,true);
aElement["radio"]		=new Array(true,false,true);
aElement["select-one"]	=new Array(true,false,true);
aElement["select-multiple"]=new Array(true,false,true);
aElement["password"]	=new Array(true,true,true);
aElement["textarea"]	=new Array(true,true,false);
aElement["file"]		=new Array(true,true,true);
aElement["hidden"]		=new Array(false,false,true);
//submit event handler
function weClose(){
if(window.weAbort)return weAbort();
document.body.innerHTML="";
document.body.innerHTML=weMsgInit();
}
function weDialog(oForm){
var oFrm=oForm?oForm:this.form;
if(!chkElmts(oFrm))return false;
if(window.event)window.event.srcElement.blur();
var zUrl=oFrm.action;
var oSpe=null,bVal,iVal,zCgi="";
for(var i=0;i<oFrm.length;i++){	//form POSTmsg or CGIparameters
 var oEl=oFrm.elements[i];
 if(oSpe){
	if(oEl.name==oSpe.name){
	 if(oEl.type!=oSpe.type){weMsgKey(oSpe.name+"类型发生改变!"+oSpe.type+"!="+oEl.type);return false;}
	 if(oEl.checked){bVal=true;iVal|=oEl.value;}
	 continue;
	}
	if((oSpe.type=="radio")&&(!bVal)){weMsgKey("'"+oEl.name+"'没有选择!");goHere(oSpe);return false;}
	zCgi+="&"+oSpe.name+".New="+iVal;
	oSpe=null;
 }
 switch(oEl.type){
 case "radio":
 case "checkbox":
	oSpe=oEl;bVal=false;iVal=0;
	if(oEl.checked){bVal=true;iVal|=oEl.value;}
	continue;
 }
 zCgi+="&"+oEl.name+".New="+escape(oEl.value);
 if(oEl.zOrg)zCgi+="&"+oEl.name+".Org="+escape(oEl.zOrg);
}
if(oFrm["weAction"]){
 switch(oFrm["weAction"].value){
 case "新增":
	zUrl=zUrl.replace(/done.php/,"insert.php");break;
 case "修改":
	zUrl=zUrl.replace(/done.php/,"update.php");break;
 case "删除":
	zUrl=zUrl.replace(/done.php/,"delete.php");break;
 default:
	zUrl=zUrl.replace(/done.php/,"confirm.php");break;
 }

}
if(/POST/gi.test(oFrm.method))return oTop.amSend(zUrl,doneRequest,zCgi);
else return oTop.amSend(zUrl+zCgi,doneRequest);
}
// ajaxMethods
function doneRequest(zTxt){
var zText=zTxt;
if(window.okQury)return okQury(zText);
if(/^<table res=1 /.test(zText)){if(window.okPost)if(okPost(zText))zText="成功";}
return weMsg(zText);
}

// initializer
function initForm(oFrm){
oFrm.onsubmit=noSubmit;
if(!oTop){
	if(parent.amSend){oTop=parent;}
	else if(parent.parent.amSend){oTop=parent.parent;}
	else if(window.amSend){oTop=window;}
	else if(opener){if(opener.parent.amSend){oTop=opener.parent;}else{weMsg("找不到根窗口");return false;}}
	else{weMsg("找不到根窗口");return false;}
}
if(oCurElmt=initElmts(oFrm))return goHere(oCurElmt);
//alert("初始化失败! FORM="+oFrm.name);
return false;
}

var oFst=null,oLast=null;
function initElmts(oFrm){
oFst=null;oLast=null;
for(var i=0;i<oFrm.length;i++){
	if(initElmt(oFrm.elements[i],oFrm))continue;
	weMsg("初始化失败! Element="+oFrm.elements[i].name);
	return false;
}
if(oLast)oLast.oNext=oFst;
return oFst;
}

function setReadOnly(oEl){
oEl.readOnly=true;
oEl.className="readOnly";
return oEl.bChk=true;
}

function initElmt(oEl,oFrm){
if(oEl.type=="hidden")return true;
if(oEl.zOrg){oEl.zOrg=oEl.value;}
if(oEl.readOnly)return setReadOnly(oEl);
if(oEl.oFst)oFst=oEl;
else{if(oFst==null)oFst=oEl;}
if(oLast){oLast.oNext=oEl;oLast=null;}
if(!oEl.zNext)oLast=oEl;
else{
	if(oFrm[oEl.zNext])oEl.oNext=oFrm[oEl.zNext];
	else{weMsg(oEl.name+"."+oEl.zNext+"不存在");return false;}
}
if(!aElement[oEl.type][2]){
 oEl.bChk=true;
 if(!oEl.className)oEl.className="button";
 switch(oEl.name){
 case "weDialog":
 case "weAction":
	if(!oEl.onfocus)oEl.onfocus=weDialog;
	return true;
 case "weClose":
	if(!oEl.onclick)oEl.onclick=weClose;
	return true;
 default:
	return true;
 }
}
if(!oEl.zReg)return setReadOnly(oEl);
oEl.bChk=false;
if(!oEl.onblur)oEl.onblur=blurElmt;
if(!oEl.onkeypress)oEl.onkeypress=keyElmt;
if(!oEl.onfocus)oEl.onfocus=focusElmt;
return true;
}

//event handler
function noSubmit(){
return false;
}

function keyElmt(oEl){
var el=oEl?oEl:this;
if(window.event.keyCode!=13)return true;
if(aElement[el.type][2]){
	el.value=el.value;
	if(!chkElmt(el)){goHere(oCurElmt);return false;}
	if(el.onchange)el.onchange(el);
	if(el.oNext)goHere(el.oNext);
	return true;
}
if(el.onclick)return el.onclick();
return true;
}

function blurElmt(){
if(this.value=="")return true;
if(chkElmt(this))return true;
goHere(this);
return false;
}

function focusElmt(){
if(oCurElmt.value=="")return goHere(this);
if(oCurElmt.bChk)return goHere(this);
if(chkElmt(oCurElmt))return goHere(this);
goHere(oCurElmt);
return false;
}

// checker
function chkElmts(oFrm){
for(var i=0;i<oFrm.length;i++){
 oEl=oFrm.elements[i];
 if(!oEl.zReg)continue;
 if(chkElmt(oEl))continue;
 goHere(oEl);
 return false;
}//End of for
if(window.chkMore){return chkMore(oFrm);}
return true;
}

function chkElmt(oEl){
var el=oEl?oEl:this;
if(!aElement[el.type][2])return true;
var oTd=el.parentElement;
var oTr=oTd.parentElement;
el.bChk=false;
var zName=oTr.cells(oTd.cellIndex-1).innerText+": ";
if(el.vChk){
	alert(el.vChk+"("+el+")");
	switch(eval(el.vChk+"(el)")){
	case 0:return false;
	case 1:el.value=el.value;return	el.bChk=true;
	default:break;
}}
if(!el.zReg){weMsgKey(zName+"没有 oReg 表达式");return false;}
switch(el.zReg){
case "noChk":	//no check need
	break;
case "date0":	//allow no value or must be date
	if(/^$/.test(el.value))	break;
case "date":	//must be date
	if(!chkDate(el))return false;
	break;
case "time0":	//allow no value or must be time
	if(/^$/.test(el.value))	break;
case "time":	//must be time
	if(!chkTime(el))return false;
	break;
case "barCode0"://allow no value or must be bar code
	if(/^$/.test(el.value))	break;
case "barCode":	//must be bar code
	if(!chkBarCode(el.value))return false;
	break;
case "barLead":	//must be bar code with leading code
	if(!el.barLead){weMsgKey(zName+"没有条码标识");return false;}
	if(!chkBarCode(el.value))return false;
	var oReg=RegExp("^"+el.barLead+"[0-9]+");
	if(!oReg.test(el.value)){weMsgKey(zName+"条码标识不是:"+el.barLead);return false;}
//	if(el.barMatch){el.value=el.value.match("^"+el.barMatch).toString()};
	break;
case "date":	//must be date
	if(!chkDate(el))return false;
	break;
default:
	var oReg=RegExp(el.zReg);
	if(oReg.test(el.value))break;
	switch(el.zReg){
	case	".+":	weMsgKey(zName+'至少输入一个字符');return false;
	default:		weMsgKey(zName+'输入数据错误! Reg="'+oReg+"'");return false;
}}
el.value=el.value;
return el.bChk=true;
}

function chkBarCode(zCode,bNoPmt){
var l=zCode.length;
switch(l){
case 13:
case 12:
case 8:
	break;
default:
	if(!bNoPmt)weMsgKey("条码长度不对! 请检验条码!");
	return false;
}
var iChk=zCode.charCodeAt(--l)-48;
var iCnt=0;
while(--l>=0){
	iCnt+=(zCode.charCodeAt(l)-48)*3;
	if(--l>=0)iCnt+=zCode.charCodeAt(l)-48;
}
iCnt=(10-iCnt%10)%10;
if(bNoPmt=="genChk")return iCnt.toString();
if(iCnt==iChk)
	return true;
if(!bNoPmt)weMsgKey("条码效验位错误! 请检验条码!"+iCnt);
return false;
}

function goHere(oEl){
var el=oEl?oEl:this;
if(!(aElement[el.type][0])){
	weMsgKey("不能停留元素类型:"+this.type);return false;
}
if(aElement[el.type][0])el.focus();
if(aElement[el.type][1])el.select();
oCurElmt=el;
return true;
}
function goOut(){
return oCurElmt.blur();
}
function goIn(){
return goHere(oFst);
}

function fmtDecimal(oVal,iSize){
var iLen=oVal.length;
var i=oVal.indexOf(".");
if(i>=0){
 i=iLen-i-1;
 if(i>iSize){
	return oVal.substr(0,iLen+iSize-i);
 }
 i=iSize-i;
}
else{
	oVal+=".";
	i=iSize;
}
while(i-->0){
	oVal+="0";
}
return oVal;
}

// Leap year Month days..
var aLeapMonth=[31,29,31,30,31,30,31,31,30,31,30,31];
function getDaysInMonth(iYear,iMonth){
if(iMonth!=1)
	return aLeapMonth[iMonth];
return (((iYear%4)==0)&&(((iYear%100)!=0)||((iYear%400==0))))?29:28;
}

function chkDate(oEl){
var el=oEl?oEl:this;
if(!/^\d{8}$/.test(el.value)){
	weMsgKey("日期格式:yyyymmdd");
	return false;
}
var y=parseInt(el.value.substr(0,4),10);
var m=parseInt(el.value.substr(4,2),10);
var d=parseInt(el.value.substr(6,2),10);
if((m<1)||(m>12)){
	weMsgKey("非法的 "+m+" 月");
	return false;
}
if((d<1)||(d>getDaysInMonth(y,--m))){
	weMsgKey("非法的 "+d+" 日");
	return false;
}
return true;
}
function chkTime(oEl){
var el=oEl?oEl:this;
if(!/^(([01][0-9])|(2[0-3]))/.test(el.value)){weMsgKey("小时格式:00-23;HH:MM:SS");return false;}
if(!/^[0-9][0-9]:[0-5][0-9]/.test(el.value)){weMsgKey("分钟格式:00-59;HH:MM:SS");return false;}
if(/^[0-9][0-9]:[0-5][0-9]$/.test(el.value)){el.value=el.value+":00";return true;}
if(!/^[0-9][0-9]:[0-5][0-9]:[0-5][0-9]$/.test(el.value)){weMsgKey("秒钟格式:00-59;HH:MM:SS");return false;}
return true;
}
function weMsgInit(){
if(!window.msg) return "<div id=msg class=msgOrg></div>";
return "";
}
var docOnkeydown=null;
function weMsgHide(){
msg.innerHTML="";
msg.className="msgOrg";
msg.onblur=null;
document.onkeydown=docOnkeydown;
return true;
}
function weMsgKey(zMsg){
docOnkeydown=document.onkeydown;
if(!msg){
	if(zMsg)alert(zMsg);
	return true;
}
if(!zMsg)return weMsgHide();
document.onkeydown=weMsgHide;
msg.onblur=weMsgHide;
msg.innerHTML=zMsg;
msg.className="msgSel";
msg.style.top=document.body.scrollTop + 20;
return true;
}
function weMsg(zMsg){
weMsgKey(zMsg);
if(msg){
	window.focus();
	msg.focus();
}
return true;
}
